package com.longrise.android.byjk.plugins.publicWeb;

import android.os.Bundle;
import android.webkit.JavascriptInterface;

import com.longrise.android.byjk.utils.Tracker_Google;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by callxkj on 2017/3/15.
 * 公共web页面prestener
 */

public class PublicWebPresenter extends PublicWebContract.Presenter {
    public static final String MRQD = "mrqd"; // 每日签到
    public static final String ZYRZ = "zyrz";// 执业认证
    public static final String SFRZ = "sfrz";// 身份认证
    public static final String NC = "nc";    // 昵称
    public static final String GRJJ = "grjj";// 个人简介
    public static final String CPFA = "cpfa";// 产品方案
    public static final String KTKJDL = "ktkjdl";// 快捷登录
    public static final String ZYXFX = "zyxfx";// 展业秀分享
    public static final String SCFL = "scfl";// 上传费率
    public static final String BYZP = "byzp";// 保易智评
    public static final String KHGL = "khgl";// 客户管理
    public static final String CPDP = "cpdp";// 产品点评
    public static final String GSDP = "gsdp";// 公司点评
    public static final String BXAL = "bxal";// 保险案例
    public static final String JHDB = "jhdb";// 计划对比

    @Override
    protected void init() {}

    /**
     * 设置webview标题
     */
    @JavascriptInterface
    public void setTitle(final String title) {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onNext(title);
                observableEmitter.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        if (mView != null) {
                            mView.setWebTitle(s);
                        }
                    }
                });
        mRxManager.addDisposable(subscribe);
    }

    /**
     * 跳转到每日签到
     */
    @JavascriptInterface
    public void toMRQD() {
        jumpToActivity(MRQD, null);

        addEvent("积分任务", "每日签到");
    }

    /**
     * 跳转到执业认证
     */
    @JavascriptInterface
    public void toZYRZ() {
        jumpToActivity(ZYRZ, null);

        addEvent("积分任务", "执业认证");
    }

    /**
     * 跳转到身份认证
     */
    @JavascriptInterface
    public void toSFRZ() {
        jumpToActivity(SFRZ, null);

        addEvent("积分任务", "身份认证");
    }

    /**
     * 跳转到昵称
     */
    @JavascriptInterface
    public void toNC() {
        jumpToActivity(NC, null);

        addEvent("积分任务", "修改昵称");
    }

    /**
     * 跳转到个人简介
     */
    @JavascriptInterface
    public void toGRJJ() {
        jumpToActivity(GRJJ, null);

        addEvent("积分任务", "个人简介");
    }

    /**
     * 产品方案
     */
    @JavascriptInterface
    public void toCPFA() {
        jumpToActivity(CPFA, null);

        addEvent("积分任务", "产品方案");
    }

    /**
     * 开通快捷登录
     */
    @JavascriptInterface
    public void toKTKJDL() {
        jumpToActivity(KTKJDL, null);

        addEvent("积分任务", "开通快捷登录");
    }

    /**
     * 展业秀分享
     */
    @JavascriptInterface
    public void toZYXFX() {
        jumpToActivity(ZYXFX, null);

        addEvent("积分任务", "展业秀分享");
    }

    /**
     * 上传费率
     */
    @JavascriptInterface
    public void uploadRateFile() {
        jumpToActivity(SCFL, null);
    }

    /**
     * 首页 展业工具里面各种item点击
     *
     * @param code
     */
    @JavascriptInterface
    public void toExperience(String code) {
        switch (code) {
            case "1005": //展业秀
                jumpToActivity(ZYXFX, null);
                break;
            case "1006": //保易智评
                jumpToActivity(BYZP, null);
                break;
            case "1007": //客户管理
                jumpToActivity(KHGL, null);
                break;
            case "1009": //产品点评
                jumpToActivity(CPDP, null);
                break;
            case "1015": //公司点评
                jumpToActivity(GSDP, null);
                break;
            case "1016": //保险案例
                jumpToActivity(BXAL, null);
                break;
            case "1027": //计划对比
                jumpToActivity(JHDB, null);
                break;
        }
    }


    @JavascriptInterface
    public void getDeviceInfor() {
        if(mView != null){
            mView.googleTracker();
        }
    }

    /**
     * 跳转到页面的总方法
     */
    public void jumpToActivity(final String title, final Bundle bundle) {
        Disposable subscribe = Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> observableEmitter) throws Exception {
                observableEmitter.onNext(title);
                observableEmitter.onComplete();
            }
        }).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        if (mView != null) {
                            mView.jumpToActivity(title, bundle);
                        }
                    }
                });
        mRxManager.addDisposable(subscribe);
    }

    private void addEvent(String cate, String action) {
        Tracker_Google.getInstance().addEvent(cate, action, "");
    }
}
